<?php
class TeacherServer {
	public function pagingBySearch($where="",$order='',$numberEachPage = 10){
		$count = HttpDaoManager::getTeacherDao()->searchNumber($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		$list = HttpDaoManager::getTeacherDao()->searchList($where,$order, $limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		$employee_id_list = Common::array_value_list($list,'employees_id');
		$data['employee_pairs'] = array();
		if(!empty($employee_id_list)){
			$employe_list = HttpDaoManager::getEmployeesDao()->getDataPairList($employee_id_list);
			$data['employee_pairs'] =Common::fetchPairOfList('id', 'name', $employe_list);
			unset($employe_list);
		}

		$data['list'] =$list;
		$data['page'] = $page1;
		return $data;
	}
	
	public function pagingTeacherOpenClassBySearch($where="",$order='',$numberEachPage = 10){
		$count = HttpDaoManager::getTeacherDao()->searchOpenclassNumber($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		$list = HttpDaoManager::getTeacherDao()->searchOpenclassList($where,$order, $limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		$employee_id_list = Common::array_value_list($list,'employees_id');
		$data['employee_pairs'] = array();
		if(!empty($employee_id_list)){
			$employe_list = HttpDaoManager::getEmployeesDao()->getDataPairList($employee_id_list);
			$data['employee_pairs'] =Common::fetchPairOfList('id', 'name', $employe_list);
			unset($employe_list);
		}

		$data['list'] =$list;
		$data['page'] = $page1;
		return $data;
	}
	
	public function add($data){
		$base_integral_list = array(EDUCATION_EXPERIENCE_1=>'200',EDUCATION_EXPERIENCE_2=>'400',EDUCATION_EXPERIENCE_3=>'600',EDUCATION_EXPERIENCE_4=>'1500',EDUCATION_EXPERIENCE_5=>'3000');
		$teacherData = HttpDaoManager::getTeacherDao()->getBySerialNumber($data['serial_number']);
		if (!empty($teacherData)){
			return 1;
		}
		$data['add_time'] =  time();
		
		$data['job_start_date'] = strtotime($data['job_start_date']);
		$data['job_end_date'] = strtotime($data['job_end_date']);
		
		if (is_array($data['good_course'])){
			foreach ($data['good_course'] as $k=>$v){
				$data['good_course'][$k]='|'.$v.'|';
			}
			$data['good_course'] = implode(',',$data['good_course']);
		}
		HttpDaoManager::getTeacherDao()->add($data);
		return true;
	}
	public function update($data,$id){
		$base_integral_list = array(EDUCATION_EXPERIENCE_1=>'200',EDUCATION_EXPERIENCE_2=>'400',EDUCATION_EXPERIENCE_3=>'600',EDUCATION_EXPERIENCE_4=>'1500',EDUCATION_EXPERIENCE_5=>'3000');
		$data['job_start_date'] = strtotime($data['job_start_date']);
		$data['job_end_date'] = strtotime($data['job_end_date']);
		if (is_array(Common::getStrValue($data, 'good_course'))){
			foreach ($data['good_course'] as $k=>$v){
				$data['good_course'][$k]='|'.$v.'|';
			}
			$data['good_course'] = implode(',',$data['good_course']);
		}
		
		HttpDaoManager::getTeacherDao()->update($data, $id);
		if ($data['status'] == 2  or $data['status'] == 3 ){
			HttpServerManager::getAdminServer()->updateStatusByTeacherId($id);
		}
	}
	public function deleteBatchByid($id){
		if (is_array($id)){
			$id = implode(',',$id);
		}
		HttpDaoManager::getTeacherDao()->deleteBatchByid($id);
	}
	public function info(){

		if ($_SESSION['admin']['teacher_id']==0){
			return 1;
		}
		return HttpDaoManager::getTeacherDao()->get($_SESSION['admin']['teacher_id']); 
	}
	public function userGetTeacherTime($teacherId,$date=''){
		$today  = strtotime(date('Y-m-d'));
		if ($date<$today or empty($date)){
			$date = $today;
		}
		$time=  HttpDaoManager::getTeacherStartCourseDao()->getTeacherTodayDateTime($teacherId,$date);
		$tmp = array();
		foreach ($time as $v){
			$h =  mb_substr($v['time'],0,2);
			$m = mb_substr($v['time'],2,2);
			$tmp[] =array('time'=>($h.':'.$m.'-'.$h.':'.($m+25)),'id'=>$v['id']) ;
		}
		return  json_encode($tmp);
	}
	public function lists(){
		$loginUser = HttpDaoManager::getUserDao()->get($_SESSION['user']['id']);
		$getDate = Common::getGetStrValue('date');
		if($getDate!==""&&!Common::is_number($getDate)){
			header('location:teacher.html');
			exit;
		}
		$date = empty($getDate)? strtotime(date('Y-m-d')):$getDate;
		$where = array();
		$where['date'] = $date;
		$where['time'] = Common::getPostStrValue('time')===""?"-1":Common::getPostStrValue('time');
		$where['good_course'] = isset($_POST['good_course'])?$_POST['goods_course']:"";
		$where['search'] =  Common::getPostStrValue('search');
		$where['name'] =  Common::getPostStrValue('name');
		$where['english_level'] =  Common::getPostStrValue('english_level')===""?"-1":Common::getPostStrValue('english_level');
		$where['sex'] =  Common::getPostStrValue('sex');
		
		if($date!==strtotime(date('Y-m-d'))){
			$where['search']='search';
		}
		
		if(!empty($loginUser['id'])&&$loginUser['user_kind']==USER_KIND_ACTIVE_708){
			$where['network_stat'] = NETWORK_STABLE;
		}
		
		//优先显示当天开课的,但是应选择所有开课的老师
		$numberEachPage = 16;
		$count =  HttpDaoManager::getTeacherStartCourseDao()->getListByGroupCount($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		
		$teacherClassList = HttpDaoManager::getTeacherStartCourseDao()->getListByGroup($where,$limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		$data['list'] = $teacherClassList;
		$data['page'] = $page1;
		
		$pxh_desc = array();
		$today_desc = array();
		$first_desc = array();
		$second_desc = array();
		foreach($data['list'] as $v){
			if($v['today_flg']==0){
				$pxh[]=0;
			}else{
				$pxh[]=$v['pxh'];
			}
			$today_desc[]=$v['today_flg'];
			$first_desc[]=$v['education_experience'];
			$second_desc[]=$v['word_mouth'];
		}
		
		$teacher_id_list=Common::array_value_list($data['list'] , 'id');
		
		//Today`s open class List
		
		$teacher_id_list_array = implode(',',$teacher_id_list);
		$where['teacher_id_list'] = $teacher_id_list_array;
		$where['teacher_openclass_status'] = TEACHER_CLASS_STATUS_NOTBOOKING;
		$where['confirm_status'] = TEACHER_STATUS_CONFIRMED;
		$where['book_date'] = $date;
		$where['start_book_time'] = Common::getDefaultNextOpenTimeByHalf();
		if($date!=strtotime(date('Y-m-d'))){
			$where['start_book_time'] = '1000';
		}
		$data['start_book_time'] =$where['start_book_time'];
		
		$todayTeacherOpenList = HttpDaoManager::getTeacherDao()->searchOpenclassList($where);
		$teacherOpenClass = array();
		foreach($todayTeacherOpenList as $key=>$value){
			if(isset($value['id'])&&$value['id']>0){
				if(isset($value['open_time'])&&$value['open_time']>0){
					$teacherOpenClass[$value['id']][$value['open_time']]['open_time']=$value['open_time']; 
					$teacherOpenClass[$value['id']][$value['open_time']]['teacher_class_id']=$value['teacher_class_id']; 
					$teacherOpenClass[$value['id']][$value['open_time']]['open_date']=$value['open_date']; 
				}
			}
		}
		
		$data['teacherOpenClassPair'] = $teacherOpenClass;
		array_multisort($pxh,SORT_DESC,$today_desc,SORT_DESC,$first_desc,SORT_DESC,$second_desc,SORT_DESC, $data['list'] );
		
		$data['select_time']="";
		if(isset($where['time'])&&!empty($where['time'])){
			$data['select_time'] = $where['time'];
		}
		$data['select_good_course']="0";
		$data['select_english_level']="-1";
		$data['select_sex']="0";
		if(isset($where['search'])&&$where['search']=='search'){
			if(isset($where['good_course'])&&!empty($where['good_course'])){
				$data['select_good_course'] = $where['good_course'];
			}
			
			if(isset($where['english_level'])&&intval($where['english_level'])>-1){
				$data['select_english_level'] = $where['english_level'];
			}
			
			if(isset($where['name'])&&!empty($where['name'])){
				$data['select_name'] = $where['name'];
			}
			if(isset($where['sex'])&&!empty($where['sex'])){
				$data['select_sex'] = $where['sex'];
			}
		}
	
		$time = date('Hm');
		$common  = new  Common();
		$times = $common->getTimes();
		$tmp = array();
		foreach ($times as $k=>$v){
			if (empty($date)){
				$date1 = date('Y年m月d日 ');
		
				if ($time<$k){
					$bool = true;
				}
				else{
					$bool = false;
				}
			}
			else{
				$date1 = date('Y年m月d日 ',$date);
				$bool = true;
			}
			if ($bool===true){
				$tmp[$k] = $date1.$v['name'].'~'.$v['nextName'];
			}
		}
		$data['time'] = $tmp ;
		$data['time_pair'] = Common::getTimesPairsArray();
		$data['course'] = Common::_t('course_type');
		$data['level'] = Common::_t('english_level');
		$data['check_date'] = $date;
		return $data;
	}
	
}

?>